/*
 * createTime: 2020/3/6
 * description: 本地缓存localStorage
 */

const STORAGE_EXPIRE_MAP = 'STORAGE_EXPIRE_MAP';
const storage = window.localStorage;

let Storage = {
	/*
	 * 获取缓存
	 * @param key 键名
	 */
	get: function(key) {
		let value = JSON.parse(storage.getItem(key));
		try {
			return value;
		} catch (e) {
			return null;
		}
	},
	/*
	 * 设置缓存
	 * @param key 键名
	 * @param object 键值
	 */
	set: function(key, object, expire) {
		if (object !== null && object !== undefined) {
			let value = object;
			if (typeof value !== 'string') {
				try {
					value = JSON.stringify(object);
				} catch (e) {
					value = null;
				}
			}
			if (value !== null) {
				storage.setItem(key, value);
				if (expire && expire > 0) {
					let expireAt = new Date().getTime() + expire * 1000;
					addExpire(key, expireAt);
				}
			}
		}
	},
	/*
	 * 移除
	 * @param key
	 */
	remove: function(key) {
		try {
			storage.removeItem(key);
		} catch (e) {
			throw new Error(e.message);
		}
	},
	/*
	 * 清理
	 */
	clear: function() {
		storage.clear();
	}
};

// 设置过期
function addExpire(key, expireAt) {
	let map = Storage.get(STORAGE_EXPIRE_MAP) || {};
	map[key] = expireAt;
	Storage.set(STORAGE_EXPIRE_MAP, map);
}

// 清理过期
function clearExpire() {
	let map = Storage.get(STORAGE_EXPIRE_MAP) || {};
	let now = new Date().getTime();
	for (let key in map) {
		if (map[key] * 1 < now) {
			Storage.remove(key);
			delete map[key];
		}
	}
	Storage.set(STORAGE_EXPIRE_MAP, map);
}

// 过期轮询
setInterval(function() {
	clearExpire();
}, 60000);

export default Storage